package com.dragon.mmochat.service;

import java.awt.Color;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import com.dragon.mmochat.MmochatConstant;
import com.dragon.mmochat.MmochatMain;
import com.dragon.mmochat.model.MmochatChenHao;
import com.dragon.mmochat.model.activity.MmochatSystemState;
import com.dragon.mmochat.model.attackMode.MmochatAttackModeOfAddBlood;
import com.dragon.mmochat.model.attackMode.MmochatAttackModeOfNormal;
import com.dragon.mmochat.model.attackMode.MmochatAttackModeOfRescueTeamer;
import com.dragon.mmochat.model.battle.MmochatBattle;
import com.dragon.mmochat.model.enumType.MmochatBossLevelType;
import com.dragon.mmochat.model.enumType.MmochatChenHaoType;
import com.dragon.mmochat.model.enumType.MmochatFamilyType;
import com.dragon.mmochat.model.enumType.MmochatNpcFaceType;
import com.dragon.mmochat.model.enumType.MmochatNpcItemType;
import com.dragon.mmochat.model.enumType.MmochatRoleAttributeType;
import com.dragon.mmochat.model.enumType.MmochatRoleBmpType;
import com.dragon.mmochat.model.enumType.MmochatTaskStateType;
import com.dragon.mmochat.model.enumType.MmochatTaskType;
import com.dragon.mmochat.model.enumType.MmochatTeamConditionType;
import com.dragon.mmochat.model.map.MmochatMap;
import com.dragon.mmochat.model.map.MmochatMapTransPoint;
import com.dragon.mmochat.model.role.MmochatMonsterFactory;
import com.dragon.mmochat.model.role.MmochatNpc;
import com.dragon.mmochat.model.role.MmochatPet;
import com.dragon.mmochat.model.role.MmochatPlayer;
import com.dragon.mmochat.model.task.MmochatNpcBattle;
import com.dragon.mmochat.model.task.MmochatTask;
import com.dragon.mmochat.service.MmochatBattleService.checkBattleTimeOut;
import com.dragon.mmochat.util.MmochatUtil;

//锁妖塔
public class MmochatTowerService {
	public static ScheduledExecutorService checkTowerTaskTimeOutThread = Executors
			.newScheduledThreadPool(1);

	// Map:地图ID-->护法属性
	public static Map<Integer, MmochatMonsterFactory> towerMonsters = new ConcurrentHashMap<Integer, MmochatMonsterFactory>();

	// 挑战层数最多的玩家:地图ID-->int[]{roleId, 挑战层数}
	public static Map<Integer, Integer[]> towerBester = new ConcurrentHashMap<Integer, Integer[]>();

	public void init() {
		if (!MmochatMain.openMergeServer) {
			// 创建锁妖塔地图
			createTowerMap();

			// 初始化锁妖塔配置
			initTowerBattles();

			checkTowerTaskTimeOutThread.scheduleWithFixedDelay(
					new checkTaskTimeOut(), 1, 1, TimeUnit.MINUTES);
		}
	}

	class checkTaskTimeOut implements Runnable {
		@Override
		public void run() {
			try {
				for (MmochatPlayer me : MmochatMainService.players.values()) {
					if (me != null) {
						if (me.isInBattle()) {
							continue;
						}
						MmochatTask task = me
								.getTaskByType(MmochatTaskType.挑战锁妖塔);
						if (task != null
								&& task.getTaskState() == MmochatTaskStateType.已接) {
							if (task.isInvalid()) {
								// 任务过期
								synchronized (task) {
									if (task.getTaskState() == MmochatTaskStateType.已接) {
										task
												.setTaskState(MmochatTaskStateType.已完成);
									} else {
										continue;
									}
								}
								// 结算奖励
								int levelCount = MmochatTowerService
										.getTowerLevelCount(task);
								MmochatPet pet = me.getPkPet();
								String info = "您完成了锁妖塔" + levelCount
										+ "层的挑战,获得了";
								if (task.getTaskTag() == MmochatConstant.taskTag_挑战经验通天塔) {
									int expAward = MmochatTowerService
											.getExpAwardOfTower(task);
									me.addExp(expAward);
									info += MmochatUtil.wrapColor(expAward
											+ "点经验", Color.green)
											+ "，";
									if (pet != null) {
										int petExpAward = MmochatBattleService
												.getRealExp(expAward, me, pet);
										pet.addExp(me, petExpAward);
										info += "您的宠物"
												+ pet.getName()
												+ "获得了"
												+ MmochatUtil.wrapColor(
														petExpAward + "点经验",
														Color.green) + "，";
									}
								} else {
									int daoAward = MmochatTowerService
											.getDaoAwardOfTower(task);
									// 刷道比赛
									MmochatPropService.dealDaoActivity(me,
											daoAward);

									int trueDao = MmochatBattleService
											.getRealDao(me, daoAward);
									me.addDao(trueDao);
									info += MmochatUtil.wrapColor(trueDao
											+ "天道行(削减前" + daoAward + "天)",
											Color.green)
											+ "，";
									if (pet != null) {
										int petDao = MmochatBattleService
												.getRealDaoOfLevel(daoAward,
														me, pet);
										int truePetDao = MmochatBattleService
												.getRealDao(pet, petDao);
										pet.addDao(truePetDao);
										info += "您的宠物"
												+ pet.getName()
												+ "获得了"
												+ MmochatUtil.wrapColor(
														truePetDao + "点武学",
														Color.green) + "，";
									}
								}
								info += "今天的挑战已完成!";
								MmochatFriendService
										.sendSystemMsgToPersonByFriendMessage(
												me, info);
								MmochatChatService.sendSystemMsgToPerson(me,
										info);
								// 称号判断与处理，NPC是否替换
								boolean ret = MmochatTowerService
										.dealTowerBest(me, task);
								if (ret) {
									int lifeTurn = (task.getTaskLevel()
											+ MmochatConstant.startTowerLevelReduce - 120) / 10;
									if (lifeTurn < 0) {
										lifeTurn = 0;
									}
									int pureLevel = task.getTaskLevel()
											+ MmochatConstant.startTowerLevelReduce
											- lifeTurn * 10;
									String levelInfo = "";
									if (lifeTurn > 0) {
										levelInfo += lifeTurn + "转";
									}
									levelInfo += pureLevel + "级";

									String bestInfo = MmochatUtil.wrapColor(me
											.getName(), Color.green)
											+ "完成了"
											+ MmochatUtil.wrapColor(levelCount
													+ "层", Color.yellow)
											+ "的锁妖塔挑战，成为了"
											+ levelInfo
											+ "中挑战层数最多的强者!";
									MmochatChatService.sendSystemMsg(bestInfo);
								}
								// 存数据库
								if (levelCount > 0) {
									try {
										MmochatDao.saveExpAndDao(me);
									} catch (Exception e) {
										e.printStackTrace();
									}
								}
								MmochatTaskService.dealFinishingTask(me, task);
							}
						}
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public static int getTowerMapIdByLevel(int level) {
		return MmochatConstant.mapId_锁妖塔20级
				+ (level - MmochatConstant.minTowerLevel);
	}

	public static int getTowerBaseNpcIdByLevel(int level) {
		return MmochatConstant.npcId_towerNpcBase
				+ (level - MmochatConstant.minTowerLevel)
				* MmochatConstant.npcNumInTower;
	}

	public static int getTowerBaseNpcIdByMapId(int mapId) {
		return MmochatConstant.npcId_towerNpcBase
				+ (mapId - MmochatConstant.mapId_锁妖塔20级)
				* MmochatConstant.npcNumInTower;
	}

	// 获取挑战的经验奖励
	public static int getExpAwardOfTower(MmochatTask task) {
		int targetLevel = task.getTargetMapId() - MmochatConstant.mapId_锁妖塔20级
				+ MmochatConstant.minTowerLevel;
		int levelCount = targetLevel - task.getTaskLevel();
		if (levelCount <= 0) {
			return 0;
		} else {
			int playerLevel = task.getTaskLevel()
					+ MmochatConstant.startTowerLevelReduce;
			levelCount = Math.min(levelCount,
					MmochatConstant.maxTowerLevelCountAward);
			Map<MmochatRoleAttributeType, Double> attrMap = MmochatPetService.monsterBaseAttackData
					.get(task.getTaskLevel()
							+ MmochatConstant.startTowerLevelReduce);
			if (attrMap != null) {
				int expAward = (int) (attrMap
						.get(MmochatRoleAttributeType.杀死怪物经验)
						* MmochatConstant.getTowerExpAwardParam(playerLevel) * levelCount);
				return expAward;
			} else {
				return 0;
			}
		}
	}

	// 获取挑战的道行奖励
	public static int getDaoAwardOfTower(MmochatTask task) {
		int targetLevel = task.getTargetMapId() - MmochatConstant.mapId_锁妖塔20级
				+ MmochatConstant.minTowerLevel;
		int levelCount = targetLevel - task.getTaskLevel();
		if (levelCount <= 0) {
			return 0;
		} else {
			levelCount = Math.min(levelCount,
					MmochatConstant.maxTowerLevelCountAward);
			return levelCount * MmochatConstant.towerDaoAwardParam;
		}
	}

	// 获得挑战的层数
	public static int getTowerLevelCount(MmochatTask task) {
		int targetLevel = task.getTargetMapId() - MmochatConstant.mapId_锁妖塔20级
				+ MmochatConstant.minTowerLevel;
		return targetLevel - task.getTaskLevel();
	}

	public static String getTowerTaskDetailByLevel(int level, MmochatTask task) {
		String detail = "向锁妖塔" + level + "层的镇妖护法请教。\n";
		if (task.getTaskTag() == MmochatConstant.taskTag_挑战经验通天塔) {
			detail += MmochatUtil.wrapColor("奖励模式:经验\n", Color.yellow);
		} else {
			detail += MmochatUtil.wrapColor("奖励模式:道行\n", Color.yellow);
		}
		detail += "当前已经成功挑战"
				+ MmochatUtil.wrapColor((level - task.getTaskLevel()),
						Color.green) + "层，挑战层数越多，奖励越厚!\n\n";
		detail += MmochatUtil.wrapColor("时间限定:2小时\n", Color.yellow);
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
		detail += MmochatUtil.wrapColor("开始时间:"
				+ sdf.format(new Date(task.getAccept_time())), Color.yellow);
		return detail;
	}

	// 创建锁妖塔地图
	private void createTowerMap() {
		for (int i = MmochatConstant.minTowerLevel; i <= MmochatConstant.maxTowerLevel; i++) {
			MmochatMap map = new MmochatMap();
			map.setMapId(getTowerMapIdByLevel(i));
			map.setMapDataId(MmochatConstant.mapDataId_锁妖塔);
			map.setWalkToBattle(false);
			map.setCanTransToFriend(false);
			map.setHasWeather(false);
			map.setTeamCondition(MmochatTeamConditionType.不允许组队);
			map.setMapName("锁妖塔" + i + "层");
			// 传送点
			MmochatMapTransPoint transPoint = new MmochatMapTransPoint();
			transPoint.setX(267);
			transPoint.setY(202);
			transPoint.setTransToMapId(MmochatConstant.mapId_西岐);
			transPoint.setNewMapX(429);
			transPoint.setNewMapY(528);
			map.getTransPoints().add(transPoint);

			MmochatMapService.maps.put(map.getMapId(), map);

			// 锁妖塔传送人
			MmochatNpc npc = new MmochatNpc();
			npc.setNpcId(MmochatConstant.npcId_towerTransNpcBase
					+ (i - MmochatConstant.minTowerLevel));
			npc.setMapId(map.getMapId());
			npc.setX((short) 317);
			npc.setY((short) 129);
			npc.setName("传送人");
			npc.setBmpType(MmochatRoleBmpType.乞丐);
			npc.setFace(MmochatNpcFaceType.朝左);
			npc.getFunctionItems().add(MmochatNpcItemType.前往下一层塔);
			npc.getFunctionItems().add(MmochatNpcItemType.前往余杭);
			npc.getFunctionItems().add(MmochatNpcItemType.前往江宁);
			npc.getFunctionItems().add(MmochatNpcItemType.前往开封);
			map.addNpc(npc);
			MmochatNpcService.npcs.put(npc.getNpcId(), npc);

			int npcBaseId = getTowerBaseNpcIdByLevel(i);

			// 锁妖塔护法
			npc = new MmochatNpc();
			npc.setNpcId(npcBaseId);
			npc.setMapId(map.getMapId());
			npc.setX((short) 197);
			npc.setY((short) 153);
			npc.setName("镇妖护法");
			int index = i % 3;
			switch (index) {
			case 0:
				npc.setBmpType(MmochatRoleBmpType.阐教女图);
				break;
			case 1:
				npc.setBmpType(MmochatRoleBmpType.人教女图);
				break;
			case 2:
				npc.setBmpType(MmochatRoleBmpType.截教女图);
			default:
				break;
			}
			npc.setFace(MmochatNpcFaceType.朝右);
			npc.getFunctionItems().add(MmochatNpcItemType.向镇妖护法请教);
			map.addNpc(npc);
			MmochatNpcService.npcs.put(npc.getNpcId(), npc);

		}
	}

	// 初始化锁妖塔战斗NPC数值
	private void initTowerBattles() {
		for (int level = MmochatConstant.minTowerLevel; level <= MmochatConstant.maxTowerLevel; level++) {
			int mapId = getTowerMapIdByLevel(level);

			MmochatMonsterFactory boss = new MmochatMonsterFactory();
			towerMonsters.put(mapId, boss);

			int index = level % 3;
			switch (index) {
			case 0:
				boss.setBmpType(MmochatRoleBmpType.阐教女图);
				boss.setFamily(MmochatFamilyType.阐教);
				break;
			case 1:
				boss.setBmpType(MmochatRoleBmpType.人教女图);
				boss.setFamily(MmochatFamilyType.人教);
				break;
			case 2:
				boss.setBmpType(MmochatRoleBmpType.截教女图);
				boss.setFamily(MmochatFamilyType.截教);
			default:
				break;
			}
			boss.setLevelType(MmochatBossLevelType.自身等级);
			boss.setLevel(level);
			boss.setDao((int) (3 * MmochatBattleService.getStandardDao(boss
					.getLevel(), 0)));
			if (level < 130) {
				boss.setEquipLevel(9);
			} else if (level < 140) {
				boss.setEquipLevel(10);
			} else if (level < 150) {
				boss.setEquipLevel(11);
			} else if (level < 160) {
				boss.setEquipLevel(12);
			} else if (level < 170) {
				boss.setEquipLevel(13);
			} else if (level < 180) {
				boss.setEquipLevel(14);
			} else if (level < 190) {
				boss.setEquipLevel(15);
			} else {
				boss.setEquipLevel(20);
			}
			boss.setTeamNum(1);
			int battleTypeIndex = level % 6;
			switch (battleTypeIndex) {
			case 0: {
				/**** 寂破:抗法，血多 ****/
				// 名字
				boss.setName("寂破");
				// 血量
				boss.setKillRound(4);
				// 物伤系数
				boss.setPhysical(0.9);
				// 法伤系数
				boss.setMagic(0.9);
				// 速度系数
				boss.setSpeedPointAddType(2);
				// 读取物理免疫
				boss.setPhysicalDefendPercent(0);
				// 读取法术免疫
				boss.setMagicDefendPercent(60);

				// 攻击方式
				MmochatAttackModeOfNormal normalAttackMode = new MmochatAttackModeOfNormal();
				normalAttackMode.setPhysicalAttackOpportunity(60);
				normalAttackMode.setMagicAttackOppotunity(30);
				normalAttackMode.setLimitOppotunity(10);
				boss.setAttackMode_normal(normalAttackMode);

				// 读取自己补血模式
				MmochatAttackModeOfAddBlood addBloodAttackMode = new MmochatAttackModeOfAddBlood();
				addBloodAttackMode.setTriggerBloodPercent(20);
				addBloodAttackMode.setAddBloodOpportunity(70);
				addBloodAttackMode.setAddBloodPercent(20);
				boss.setAttackMode_addBlood(addBloodAttackMode);
				break;
			}
			case 1: {
				/**** 冥风:速度超快，伤害超高，血超少 ****/
				// 名字
				boss.setName("冥风");
				// 血量
				boss.setKillRound(0.5);
				// 物伤系数
				boss.setPhysical(2);
				// 法伤系数
				boss.setMagic(1.6);
				// 速度系数
				boss.setSpeedPointAddType(6);
				// 读取物理免疫
				boss.setPhysicalDefendPercent(0);
				// 读取法术免疫
				boss.setMagicDefendPercent(0);

				// 攻击方式
				MmochatAttackModeOfNormal normalAttackMode = new MmochatAttackModeOfNormal();
				normalAttackMode.setPhysicalAttackOpportunity(60);
				normalAttackMode.setMagicAttackOppotunity(30);
				normalAttackMode.setLimitOppotunity(10);
				boss.setAttackMode_normal(normalAttackMode);
				break;
			}
			case 2: {
				/**** 奚仲:抗物，血多 ****/
				// 名字
				boss.setName("奚仲");
				// 血量
				boss.setKillRound(4);
				// 物伤系数
				boss.setPhysical(0.9);
				// 法伤系数
				boss.setMagic(0.9);
				// 速度系数
				boss.setSpeedPointAddType(2);
				// 读取物理免疫
				boss.setPhysicalDefendPercent(60);
				// 读取法术免疫
				boss.setMagicDefendPercent(0);

				// 攻击方式
				MmochatAttackModeOfNormal normalAttackMode = new MmochatAttackModeOfNormal();
				normalAttackMode.setPhysicalAttackOpportunity(60);
				normalAttackMode.setMagicAttackOppotunity(30);
				normalAttackMode.setLimitOppotunity(10);
				boss.setAttackMode_normal(normalAttackMode);

				// 读取自己补血模式
				MmochatAttackModeOfAddBlood addBloodAttackMode = new MmochatAttackModeOfAddBlood();
				addBloodAttackMode.setTriggerBloodPercent(20);
				addBloodAttackMode.setAddBloodOpportunity(70);
				addBloodAttackMode.setAddBloodPercent(20);
				boss.setAttackMode_addBlood(addBloodAttackMode);
				break;
			}
			case 3: {
				/**** 归邪:速度快，经常用障碍技能 ****/
				// 名字
				boss.setName("归邪");
				// 血量
				boss.setKillRound(2);
				// 物伤系数
				boss.setPhysical(0.9);
				// 法伤系数
				boss.setMagic(0.9);
				// 速度系数
				boss.setSpeedPointAddType(4);
				// 读取物理免疫
				boss.setPhysicalDefendPercent(0);
				// 读取法术免疫
				boss.setMagicDefendPercent(0);

				// 攻击方式
				MmochatAttackModeOfNormal normalAttackMode = new MmochatAttackModeOfNormal();
				normalAttackMode.setPhysicalAttackOpportunity(30);
				normalAttackMode.setMagicAttackOppotunity(30);
				normalAttackMode.setLimitOppotunity(40);
				boss.setAttackMode_normal(normalAttackMode);
				break;
			}
			case 4: {
				/**** 赢幽:速度慢，法攻高 ****/
				// 名字
				boss.setName("赢幽");
				// 血量
				boss.setKillRound(2);
				// 物伤系数
				boss.setPhysical(0.4);
				// 法伤系数
				boss.setMagic(1.6);
				// 速度系数
				boss.setSpeedPointAddType(1);
				// 读取物理免疫
				boss.setPhysicalDefendPercent(0);
				// 读取法术免疫
				boss.setMagicDefendPercent(0);

				// 攻击方式
				MmochatAttackModeOfNormal normalAttackMode = new MmochatAttackModeOfNormal();
				normalAttackMode.setPhysicalAttackOpportunity(10);
				normalAttackMode.setMagicAttackOppotunity(80);
				normalAttackMode.setLimitOppotunity(10);
				boss.setAttackMode_normal(normalAttackMode);
				break;
			}
			case 5: {
				/**** 击残:速度最慢，物攻高 ****/
				// 名字
				boss.setName("击残");
				// 血量
				boss.setKillRound(2);
				// 物伤系数
				boss.setPhysical(2);
				// 法伤系数
				boss.setMagic(0.4);
				// 速度系数
				boss.setSpeedPointAddType(0);
				// 读取物理免疫
				boss.setPhysicalDefendPercent(0);
				// 读取法术免疫
				boss.setMagicDefendPercent(0);

				// 攻击方式
				MmochatAttackModeOfNormal normalAttackMode = new MmochatAttackModeOfNormal();
				normalAttackMode.setPhysicalAttackOpportunity(80);
				normalAttackMode.setMagicAttackOppotunity(10);
				normalAttackMode.setLimitOppotunity(10);
				boss.setAttackMode_normal(normalAttackMode);
				break;
			}
			default:
				break;
			}

			// 读取技能目标数
			boss.setBossMaxSkillTargetNum(2);
			// 读取封印抵抗
			boss.setAntiSeal(10);
			// 读取冰冻抵抗
			boss.setAntiFreeze(10);
			// 读取混乱抵抗
			boss.setAntiConfuse(10);
			// 读取神圣物理伤害
			boss.setSuperPhysicalAttack(20);
			// 读取神圣法术伤害
			boss.setSuperSpritAttack(20);
			// 读取是否能复活
			boss.setCanRescure(true);
		}
	}

	// 读取随机的锁妖塔宠物属性
	public static MmochatMonsterFactory getRandomTowerPetByLevel(int mapId,
			int dao) {
		int level = 20 + (mapId - MmochatConstant.mapId_锁妖塔20级);
		int index = MmochatUtil.getRandomValue(4);
		MmochatMonsterFactory boss = new MmochatMonsterFactory();
		switch (index) {
		case 0: {
			// 血宠
			boss.setName("铁血玄武");
			// 血量
			boss.setKillRound(4.5);
			// 物伤系数
			boss.setPhysical(0.4);
			// 法伤系数
			boss.setMagic(0.3);
			// 速度系数
			boss.setSpeedPointAddType(1);
			// 读取物理免疫
			boss.setPhysicalDefendPercent(0);
			// 读取法术免疫
			boss.setMagicDefendPercent(0);

			// 攻击方式
			MmochatAttackModeOfNormal normalAttackMode = new MmochatAttackModeOfNormal();
			normalAttackMode.setPhysicalAttackOpportunity(50);
			normalAttackMode.setMagicAttackOppotunity(50);
			boss.setAttackMode_normal(normalAttackMode);

			// 读取自己补血模式
			MmochatAttackModeOfAddBlood addBloodAttackMode = new MmochatAttackModeOfAddBlood();
			addBloodAttackMode.setTriggerBloodPercent(10);
			addBloodAttackMode.setAddBloodOpportunity(50);
			addBloodAttackMode.setAddBloodPercent(10);
			boss.setAttackMode_addBlood(addBloodAttackMode);

			// 读取救助队友模式
			MmochatAttackModeOfRescueTeamer rescueTeamerAttackMode = new MmochatAttackModeOfRescueTeamer();
			rescueTeamerAttackMode.setRescueOpportunity(100);
			rescueTeamerAttackMode.setBloodPercent(10);
			boss.setAttackMode_rescueTeamer(rescueTeamerAttackMode);
			break;
		}
		case 1: {
			// 敏宠
			boss.setName("幻影玄武");
			// 血量
			boss.setKillRound(3);
			// 物伤系数
			boss.setPhysical(0.5);
			// 法伤系数
			boss.setMagic(0.5);
			// 速度系数
			boss.setSpeedPointAddType(5);
			// 读取物理免疫
			boss.setPhysicalDefendPercent(0);
			// 读取法术免疫
			boss.setMagicDefendPercent(0);

			// 攻击方式
			MmochatAttackModeOfNormal normalAttackMode = new MmochatAttackModeOfNormal();
			normalAttackMode.setPhysicalAttackOpportunity(50);
			normalAttackMode.setMagicAttackOppotunity(50);
			boss.setAttackMode_normal(normalAttackMode);

			// 读取自己补血模式
			MmochatAttackModeOfAddBlood addBloodAttackMode = new MmochatAttackModeOfAddBlood();
			addBloodAttackMode.setTriggerBloodPercent(10);
			addBloodAttackMode.setAddBloodOpportunity(50);
			addBloodAttackMode.setAddBloodPercent(10);
			boss.setAttackMode_addBlood(addBloodAttackMode);

			// 读取救助队友模式
			MmochatAttackModeOfRescueTeamer rescueTeamerAttackMode = new MmochatAttackModeOfRescueTeamer();
			rescueTeamerAttackMode.setRescueOpportunity(100);
			rescueTeamerAttackMode.setBloodPercent(10);
			boss.setAttackMode_rescueTeamer(rescueTeamerAttackMode);
			break;
		}
		case 2: {
			// 力宠
			boss.setName("高力玄武");
			// 血量
			boss.setKillRound(3);
			// 物伤系数
			boss.setPhysical(1.2);
			// 法伤系数
			boss.setMagic(0.3);
			// 速度系数
			boss.setSpeedPointAddType(2);
			// 读取物理免疫
			boss.setPhysicalDefendPercent(0);
			// 读取法术免疫
			boss.setMagicDefendPercent(0);

			// 攻击方式
			MmochatAttackModeOfNormal normalAttackMode = new MmochatAttackModeOfNormal();
			normalAttackMode.setPhysicalAttackOpportunity(80);
			normalAttackMode.setMagicAttackOppotunity(20);
			boss.setAttackMode_normal(normalAttackMode);

			// 读取自己补血模式
			MmochatAttackModeOfAddBlood addBloodAttackMode = new MmochatAttackModeOfAddBlood();
			addBloodAttackMode.setTriggerBloodPercent(10);
			addBloodAttackMode.setAddBloodOpportunity(50);
			addBloodAttackMode.setAddBloodPercent(10);
			boss.setAttackMode_addBlood(addBloodAttackMode);

			// 读取救助队友模式
			MmochatAttackModeOfRescueTeamer rescueTeamerAttackMode = new MmochatAttackModeOfRescueTeamer();
			rescueTeamerAttackMode.setRescueOpportunity(80);
			rescueTeamerAttackMode.setBloodPercent(10);
			boss.setAttackMode_rescueTeamer(rescueTeamerAttackMode);
			break;
		}
		case 3:
		default: {
			// 法宠
			boss.setName("通灵玄武");
			// 血量
			boss.setKillRound(3);
			// 物伤系数
			boss.setPhysical(0.3);
			// 法伤系数
			boss.setMagic(1);
			// 速度系数
			boss.setSpeedPointAddType(2);
			// 读取物理免疫
			boss.setPhysicalDefendPercent(0);
			// 读取法术免疫
			boss.setMagicDefendPercent(0);

			// 攻击方式
			MmochatAttackModeOfNormal normalAttackMode = new MmochatAttackModeOfNormal();
			normalAttackMode.setPhysicalAttackOpportunity(20);
			normalAttackMode.setMagicAttackOppotunity(80);
			boss.setAttackMode_normal(normalAttackMode);

			// 读取自己补血模式
			MmochatAttackModeOfAddBlood addBloodAttackMode = new MmochatAttackModeOfAddBlood();
			addBloodAttackMode.setTriggerBloodPercent(10);
			addBloodAttackMode.setAddBloodOpportunity(50);
			addBloodAttackMode.setAddBloodPercent(10);
			boss.setAttackMode_addBlood(addBloodAttackMode);

			// 读取救助队友模式
			MmochatAttackModeOfRescueTeamer rescueTeamerAttackMode = new MmochatAttackModeOfRescueTeamer();
			rescueTeamerAttackMode.setRescueOpportunity(80);
			rescueTeamerAttackMode.setBloodPercent(10);
			boss.setAttackMode_rescueTeamer(rescueTeamerAttackMode);
			break;
		}
		}
		boss.setBmpType(MmochatRoleBmpType.玄武);
		boss.setFamily(MmochatFamilyType.截教);
		boss.setLevelType(MmochatBossLevelType.自身等级);
		boss.setLevel(level);
		boss.setDao(dao);
		if (level < 130) {
			boss.setEquipLevel(9);
		} else if (level < 140) {
			boss.setEquipLevel(10);
		} else if (level < 150) {
			boss.setEquipLevel(11);
		} else if (level < 160) {
			boss.setEquipLevel(12);
		} else if (level < 170) {
			boss.setEquipLevel(13);
		} else if (level < 180) {
			boss.setEquipLevel(14);
		} else if (level < 190) {
			boss.setEquipLevel(15);
		} else {
			boss.setEquipLevel(20);
		}
		boss.setTeamNum(1);
		// 读取技能目标数
		boss.setBossMaxSkillTargetNum(2);
		// 读取封印抵抗
		boss.setAntiSeal(10);
		// 读取冰冻抵抗
		boss.setAntiFreeze(10);
		// 读取混乱抵抗
		boss.setAntiConfuse(10);
		// 读取神圣物理伤害
		boss.setSuperPhysicalAttack(20);
		// 读取神圣法术伤害
		boss.setSuperSpritAttack(20);
		// 读取是否能复活
		boss.setCanRescure(false);
		return boss;
	}

	// 判断玩家是否超越前者，成为本级别挑战层数最多的玩家。返回true则超越，false未超越
	public static boolean dealTowerBest(MmochatPlayer me, MmochatTask task) {
		synchronized (towerBester) {
			int levelCount = getTowerLevelCount(task);
			if (levelCount == 0) {
				return false;
			}
			int startLevel = task.getTaskLevel()
					+ MmochatConstant.startTowerLevelReduce;
			int mapId = getTowerMapIdByLevel(startLevel);
			Integer[] values = towerBester.get(mapId);
			if (values == null) {
				values = new Integer[2];
				values[0] = me.getRoleId();
				values[1] = levelCount;
				towerBester.put(mapId, values);

				// 称号处理
				MmochatChenHao chenhao = new MmochatChenHao();
				chenhao.setType(MmochatChenHaoType.镇妖护法);
				chenhao.setExtraEffectInfo("同级别中挑战锁妖塔层数最多的人才能得到的荣誉!");
				MmochatChenHao.addAndUpdateChenHaoAndSaveSql(me.getRoleId(),
						chenhao);
			} else {
				if (values[1] >= levelCount) {
					return false;
				} else {
					if (values[0] != me.getRoleId()) {
						// 称号处理
						MmochatChenHao.delAndUpdateChenHaoAndSaveSql(values[0],
								MmochatChenHaoType.镇妖护法);
						MmochatChenHao chenhao = new MmochatChenHao();
						chenhao.setType(MmochatChenHaoType.镇妖护法);
						chenhao.setExtraEffectInfo("同级别中挑战锁妖塔层数最多的人才能得到的荣誉!");
						MmochatChenHao.addAndUpdateChenHaoAndSaveSql(me
								.getRoleId(), chenhao);
					}
					values[0] = me.getRoleId();
					values[1] = levelCount;
					towerBester.put(mapId, values);
				}
			}
			// NPC处理
			int npcId = getTowerBaseNpcIdByMapId(mapId);
			MmochatNpc npc = MmochatNpcService.npcs.get(npcId);
			if (npc != null) {
				npc.setName(me.getName());
				npc.setBmpType(me.getBmpType());
			}
			MmochatMonsterFactory boss = towerMonsters.get(mapId);
			if (boss != null) {
				boss.setBmpType(me.getBmpType());
				boss.setDao(me.getDao());
				boss.setFamily(me.getFamily());
			}
		}
		return true;
	}

	public static void delAllTowerChenhao() {
		for (Integer[] values : towerBester.values()) {
			if (values != null) {
				int roleId = values[0];
				MmochatPlayer me = MmochatGmService.getRoleInfoById(roleId);
				if (me == null) {
					continue;
				}
				me.getChenHaoMap().remove(MmochatChenHaoType.镇妖护法);
				try {
					MmochatDao.saveChenhao(me);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}

}
